//
//  MFWHttpParametersBuilder.m
//  MFWSdk
//
//  Created by Song Xiaofeng on 13-8-6.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import "MFWHttpParametersBuilder.h"
#import "NSString+EnDeCoding.h"

NSString *const kComposeParmameterName = @"s";

@implementation MFWHttpParametersBuilder
/**
 *	对原始参数添加公共参数
 *
 *	@param	parameterDic	out parameter which will contains public parameters when returned
 *
 *	@return	Success or not
 */
+ (BOOL)getPublicParameterFilledDictionary:(NSMutableDictionary*)parameterDic;
{
    if (parameterDic && [parameterDic isKindOfClass:[NSMutableDictionary class]])
    {
        MFWSdkEnvironment *environment = [MFWSdkEnvironment sharedInstance];
        
        [parameterDic setValue:@"ios" forKey:@"device_type"];
        [parameterDic setValue:environment.appCode forKey:@"app_code"];
        [parameterDic setValue:environment.openUDID forKey:@"open_udid"];
        [parameterDic setValue:environment.channelId forKey:@"channel_id"];
        [parameterDic setValue:environment.isDEBUG ? @"1":@"0" forKey:@"debug"];
        [parameterDic setValue:environment.appVersion forKey:@"app_ver"];
        [parameterDic setValue:environment.systemVersion forKey:@"sys_ver"];
        [parameterDic setValue:environment.mfwSDKVersion forKey:@"ywsdk_ver"];
        [parameterDic setValue:environment.hardwareModel forKey:@"hardware_model"];
        [parameterDic setValue:environment.ver forKey:@"ver"];
        [parameterDic setValue:environment.idfa forKey:@"idfa"];
        
        if (environment.hasLogin)
        {
            [parameterDic setValue:environment.userInfo.uid forKey:@"uid"];
        }
        
        if (environment.pushDeviceToken)
        {
            [parameterDic setValue:environment.pushDeviceToken forKey:@"device_token"];
        }
        
        return YES;
    }
    else
    {
        return NO;
    }
}


/**
 *	对参数进行内部业务签名，并添加公共参数
 *  最后得到形如 s => {"data":{}, "sign":""} ,其中sign可选，
 *  当serviceSecret为空时，不做签名，此时没有sign, 结果为 s => {"data":{}}
 *
 *	@param	originalParams	原始参数字典
 *	@param	shouldFillPublicParameters	是否需要向原始参数字典里添加公共参数
 *	@param	serviceSecret	业务签名密钥，可以为空
 *
 *	@return	组装后的参数字典其中只有一个参数 s => {"data":{}, "sign":""}
 */
+ (NSMutableDictionary*)composeParameterS:(NSDictionary*)originalParams
                 autoFillPublicParameters:(BOOL)shouldFillPublicParameters
                        serviceSignSecret:(NSString*)serviceSecret;
{
    NSMutableDictionary *originalDics = [NSMutableDictionary dictionaryWithDictionary:originalParams];
    
    // add public common parameters
    if (shouldFillPublicParameters)
    {
        [self getPublicParameterFilledDictionary:originalDics];
    }

    NSDictionary *composedParameters = nil;
    if (![MFWStringUtils isEmptyString:serviceSecret])
    {
        NSString *dataValue = [MFWJsonUtils stringEncodingJsonObject:originalDics];
        
        NSString *innerSign = [[NSString stringWithFormat:@"%@%@", dataValue, serviceSecret] md5Encode];
        
        composedParameters = @{@"data": originalDics, @"sign":innerSign};
    }
    else
    {
        composedParameters = @{@"data": originalDics};
    }
    
    
    NSMutableDictionary *resultDic = [NSMutableDictionary dictionaryWithObject:[MFWJsonUtils stringEncodingJsonObject:composedParameters] forKey:kComposeParmameterName];
    
    return resultDic;
}

/**
 *	同 [MFWHttpParametersBuilder composeParameterS:orignalParams autoFillPublicParameters:shouldFillPublicParameters serviceSignSecret:nil]
 *
 */
+ (NSMutableDictionary*)composeParameterS:(NSDictionary*)originalParams
                 autoFillPublicParameters:(BOOL)shouldFillPublicParameters;
{
    return [self composeParameterS:originalParams autoFillPublicParameters:shouldFillPublicParameters serviceSignSecret:nil];
}

/**
 *  *	同 [MFWHttpParametersBuilder composeParameterS:orignalParams autoFillPublicParameters:YES]
 */
+ (NSMutableDictionary*)composeParameterS:(NSDictionary*)originalParams;
{
    return [self composeParameterS:originalParams autoFillPublicParameters:YES];
}
@end
